package com.example.server.service.impl;

import com.example.common.pojo.Department;
import com.example.server.mapper.DepartmentMapper;
import com.example.server.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小红
 * @since 2022-09-02
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public List<Department> getDeptLevel() {
        return departmentMapper.findDeptLevel(-1);
    }

    @Override
    public RespBean addDept(Department department) {

        departmentMapper.callAddDept(department);

        if (department.getResult() != 1) {
            return RespBean.error(502,"新增部门失败");
        }else {
            return RespBean.success(department);
        }
    }

    @Override
    public RespBean DeleteDept(Integer id) {
       return null;
    }

    @Override
    public RespBean deleteDepartment(Integer id) {
        Department department = new Department();
        department.setId(id);
        departmentMapper.deleteDept(department);

        if (-2 == department.getResult()) {
            return RespBean.error(502,"该部门下还有子部门，删除失败");
        }

        if (-1 == department.getResult()) {
            return RespBean.error(502,"该部门下还有员工，删除失败");
        }

        if (1 == department.getResult()) {
            return RespBean.success("删除成功");
        }

        return RespBean.error(502,"删除失败");
    }

}
